package com.quickdv.helper;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.quickdv.helper.imp.NetWorkInfo;
import com.quickdv.network.HttpConnect;
import com.quickdv.until.LogHelper;


/**
 * Created by lilongfei on 15/9/4.
 */
public abstract class NetWorkImp implements NetWorkInfo {

    HttpConnect network;
    protected Context c;
    protected LogHelper log;
    protected String tag;

    public NetWorkImp(Context c){
        this.network = new HttpConnect(c,this);
        this.c = c;
        log = LogHelper.getInstance();
    }

    public boolean isOk(JSONObject obj)  {
        if(obj == null){
            return false;
        }else if(obj.getString("responseError") == null){
            return false;
        }else if(obj.getString("responseError").equals("200")){
            return true;
        }
        return false;
    }

    @Override
    public void onFastJsonResponse(Object tag, com.alibaba.fastjson.JSONObject result) {
        log.v(tag+"",result!=null ? result.toString() : " result 是空的");
        onRequestSuccess(tag,result);
    }

    @Override
    public void addFastJsonQueue(int type, String method, JSONObject params) {
        this.tag = method;
        addFastJsonQueue(type, method, method, params);
    }

    @Override
    public void addFastJsonQueue(int type, String tag, String method, JSONObject params) {
        this.tag = tag;
        network.addToRequestQueue(type, tag, method, params);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        onRequestExcption(error);
        network.stopRequest();
    }


    @Override
    public void startRequest() {
        network.start();
    }

    protected abstract void onRequestSuccess(Object tag, JSONObject result);
    protected abstract void onRequestExcption(VolleyError error);
}
